/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.cloud.feign.interceptor;

import cn.hutool.core.map.MapUtil;
import com.iwindplus.boot.web.manager.context.ContextTransManager;
import com.iwindplus.cloud.feign.domain.property.FeignProperty;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.annotation.Resource;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
 * Feign请求过滤器.
 *
 * @author zengdegui
 * @since 2020/4/23
 */
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {

    @Resource
    private FeignProperty property;

    @Resource
    private ContextTransManager contextTransManager;

    @Override
    public void apply(RequestTemplate template) {
        if (Boolean.FALSE.equals(this.property.getRequest().getEnabled())) {
            return;
        }
        Map<String, String> map = contextTransManager.buildRequestHeaderInMdc(null);
        if (MapUtil.isEmpty(map)) {
            return;
        }

        for (Map.Entry<String, String> entry : map.entrySet()) {
            template.header(entry.getKey(), entry.getValue());
        }
    }
}
